import { createParamDecorator, ExecutionContext } from '@nestjs/common'
import { AuthUser } from '../interfaces/jwt-payload.interface'

/**
 * 当前用户装饰器
 * 用于在控制器方法中获取当前认证用户信息
 */
export const CurrentUser = createParamDecorator(
  (data: keyof AuthUser | undefined, ctx: ExecutionContext): AuthUser | any => {
    const request = ctx.switchToHttp().getRequest()
    const user: AuthUser = request.user

    // 如果指定了特定字段，则返回该字段的值
    return data ? user?.[data] : user
  }
)
